//trigger updateApplication on Application__c (before insert, before update)
trigger updateApplication on Application__c (after insert, before update, after update)
{
    
    
    //For SLA Implementation.... (Record Creation)
    if(Trigger.isInsert && Trigger.isAfter )
    {
    
        List<SLA__c> slaList = new List<SLA__c>();
         
        // Assumes the BSC work hours are the default for the Org
        List<BusinessHours> businessHoursObjectList = new List<BusinessHours>([select id from businesshours where isDefault = true LIMIT 1]) ;
    
        if(businessHoursObjectList.size() > 0)
        {
            for(Application__c applicationObject : Trigger.New)
            {
                SLA__c slaObject = new SLA__c();
                slaObject.SLA_Hours__c = applicationObject.SLA_Hours__c ;
                slaObject.Actual_Start_Date_Time__c = DateTime.Now();
               
                // BSC works 11.5 hours / day (8:00 AM - 7:30 PM, M-F). Our SLA is 2-days (23 business hours)
                System.debug('The Actual Start Time => '+ slaObject.Actual_Start_Date_Time__c );
                System.debug('The Expected End Date => '+ slaObject.Actual_Start_Date_Time__c.addhours(integer.valueof(applicationObject.SLA_Hours__c )));
                System.debug('The Business Hour Date => ' + BusinessHours.add(businessHoursObjectList[0].id, slaObject.Actual_Start_Date_Time__c, integer.valueof(applicationObject.SLA_Hours__c) * 60 * 60 * 1000L));
                System.debug('The Business Hour Date GMT => ' + BusinessHours.addGmt(businessHoursObjectList[0].id, slaObject.Actual_Start_Date_Time__c, integer.valueof(applicationObject.SLA_Hours__c) * 60 * 60 * 1000L));
                slaObject.Expected_End_Date__c = BusinessHours.addGmt(businessHoursObjectList[0].id, slaObject.Actual_Start_Date_Time__c, integer.valueof(applicationObject.SLA_Hours__c) * 60 * 60 * 1000L);   // DateTime.Now().addhours(slaHours);
                
                slaObject.Application__c = applicationObject.Id ;
                slaObject.Process_Type__c = 'Application';
                slaObject.Application_Type__c = applicationObject.Transaction_Type_Name__c;
                //Adding in the List...
                slaList.add(slaObject);            
            
            
            }
            
            if(slaList.size() > 0)
                upsert slaList;
            
        }
    }
    
    
    /* The trigger creates a new contact if the new Application doesn't have 
    any name of applicant assigned to it. */
    
    List<Contact> contactInsertList = new List<Contact>();   
    List<Passport_History__c> passportHistoryInsertList = new List<Passport_History__c>();   
    List<Visa_History__c> visaHistoryInsertList = new List<Visa_History__c>();
    List<Visa_History__c> visaHistoryUpdateList = new List<Visa_History__c>();
    List<Visa_History__c> visaHistoryEmploymentUpdateList = new List<Visa_History__c>();
    List<Passport_History__c> phList = new List<Passport_History__c>();   
    List<Passport_History__c> passportHistoryUpsertList = new List<Passport_History__c>();   
    
    Map<Id,Id> visaHistoryStopCompanyMap = new Map<Id,Id>();
    Map<Id,Id> visaHistoryStopAfzaMap = new Map<Id,Id>();
    Map<Id,Id> visaHistoryRejectMap = new Map<Id,Id>();
  
    Map<Id,Id> visaHistorySendImmigrationMap = new Map<Id,Id>();
    Map<Id,Id> visaHistoryApprovedMap = new Map<Id,Id>();

    Map<Id,Id> applicationPassportIdMap = new Map<Id,Id>();
    
    Set<ID> establishmentCardSet = new Set<ID>();
    Set<Id> applicantNameIdSet = new Set<Id>();
    Map<string,Application__c> applicationEstablistmentNewMap = new Map<string,Application__c>();
    Map<string,Application__c> applicationEstablistmentNewFineMap = new Map<string,Application__c>();
    
    
    if (Trigger.isUpdate && Trigger.isAfter) 
    {
        for(Application__c applicationObject : Trigger.new)
        {
         applicantNameIdSet.add(applicationObject.Name_of_Applicant__c); //added paulo nov 19
          	
          if(applicationObject.Record_Type_Name__c == 'Establishment_Card')
          {
            
            if(applicationObject.Transaction_Type_Name__c != null)
                {
                   //if(applicationObject.Immigration_Status__c != Trigger.oldMap.get(applicationObject.id).Immigration_Status__c)
                    //{
                      //if(applicationObject.Immigration_Status__c  == 'Approved')
                      if(applicationObject.Immigration_Status__c  == 'Completed')
                      {
                        if(applicationObject.Company__c != null)
                        {
                          string tempKey = applicationObject.Id + '' + applicationObject.Company__c;
                          //Adding in the Account Set...
                          establishmentCardSet.add(applicationObject.Company__c);
                          
                          //For Establishment Card...
                          if(applicationObject.Transaction_Type_Name__c == 'Establishment New')
                          {
                            //Adding in the Map...
                            applicationEstablistmentNewMap.put(tempKey, applicationObject);  
                          }  
                          else if (applicationObject.Transaction_Type_Name__c != 'Establishment New' &&  applicationObject.Transaction_Type_Name__c != 'Establishment Card Fines')
                          {
                  applicationEstablistmentNewMap.put(tempKey, applicationObject);  
                          }
                        }  
                      }  
                    //}     
                
                }
            /*if(applicationObject.isApproved__c == true)
                {
                  
                  if(applicationObject.Transaction_Type_Name__c != Trigger.oldMap.get(applicationObject.id).Transaction_Type_Name__c)
                    {
                      
                      if(applicationObject.Company__c != null)
                      {
                        string tempKey = applicationObject.Id + '' + applicationObject.Company__c;
                        //Adding in the Account Set...
                        establishmentCardSet.add(applicationObject.Company__c);
                        
                        //For Establishment Card...
                        if(applicationObject.Transaction_Type_Name__c == 'Establishment New')
                        {
                          //Adding in the Map...
                          applicationEstablistmentNewMap.put(tempKey, applicationObject);  
                        }  
                        else if (applicationObject.Transaction_Type_Name__c != 'Establishment New' &&  applicationObject.Transaction_Type_Name__c != 'Establishment Card Fines')
                        {
                applicationEstablistmentNewMap.put(tempKey, applicationObject);  
                        }
                      }
                    }
                
                }*/
            
          }
            else if(applicationObject.Record_Type_Name__c == 'Employment')
            {
                if(applicationObject.isApproved__c == true)
                {
                    if(applicationObject.Immigration_Status__c != Trigger.oldMap.get(applicationObject.id).Immigration_Status__c)
                    {
                        System.debug('The Value of Application Name => '+ applicationObject.Applicant_Visa_Id__c  ); 
                        if(applicationObject.Applicant_Visa_Id__c != null)
                        {
                            System.debug('The Name of Applicant is not null');
                            if(applicationObject.Immigration_Status__c == 'Stop - Company')
                            {
                                 visaHistoryStopCompanyMap.put(applicationObject.Applicant_Visa_Id__c, applicationObject.Applicant_Visa_Id__c);               
                            
                            }
                            
                            else if(applicationObject.Immigration_Status__c == 'Stop - AFZA')
                            {
                                visaHistoryStopAfzaMap.put(applicationObject.Applicant_Visa_Id__c , applicationObject.Applicant_Visa_Id__c);               
                                                            
                            }
                            else if(applicationObject.Immigration_Status__c == 'Normal Rejection'  || applicationObject.Immigration_Status__c == 'Rejection - Security' )
                            {
                                visaHistoryRejectMap.put(applicationObject.Applicant_Visa_Id__c , applicationObject.Applicant_Visa_Id__c);                                           
                            
                            }    
                            
                            else if ( applicationObject.Immigration_Status__c == 'Sent to immigration')
              {
                visaHistorySendImmigrationMap.put(applicationObject.Applicant_Visa_Id__c , applicationObject.Applicant_Visa_Id__c);
              }
              else if (applicationObject.Immigration_Status__c == 'Approved')
              {
                visaHistoryApprovedMap.put(applicationObject.Applicant_Visa_Id__c , applicationObject.Applicant_Visa_Id__c);
              }
                            
                            
                            
                        }
                    } 
                }
            }//end if employment
        }//end for application loop
        
        /*Added by Paulo nov 19, Employment Application reaches the Status of Completed   */  
        Map<Id, Passport_History__c> passHistoryMap = new Map<Id, Passport_History__c>([SELECT id FROM Passport_History__c WHERE IsCurrent__c = true AND Passport_Holder__c IN : applicantNameIdSet]);      
        for(Application__c applicationObject : Trigger.new){
        	if(applicationObject.Record_Type_Name__c == 'Employment'){
        		 if(applicationObject.Immigration_Status__c != Trigger.oldMap.get(applicationObject.id).Immigration_Status__c && applicationObject.Immigration_Status__c == 'Completed') {
	                        List<Visa_History__c> vhList = [SELECT id, Visa_Number__c, Visa_Issue_Date__c, Visa_Expiry_Date__c, Visa_Holder__c FROM Visa_History__c WHERE Visa_Holder__c =: applicationObject.Name_of_Applicant__c AND IsCurrent__c = true];
	                        for(Visa_History__c vh : vhList){
	                        	vh.Visa_Number__c = applicationObject.Residency_File_Number__c;
								vh.Visa_Issue_Date__c = applicationObject.Entry_Permit_Issue_Date__c;
								vh.Visa_Expiry_Date__c = applicationObject.Entry_Permit_Expiry_Date__c;
								vh.MOI_Number__c = integer.valueOf(applicationObject.Unified_ID__c);
	                        	visaHistoryEmploymentUpdateList.add(vh);
	                        }
	              }
        	} 
        	else if(applicationObject.Record_Type_Name__c == 'Residency')
            {
                    if(applicationObject.Immigration_Status__c != Trigger.oldMap.get(applicationObject.id).Immigration_Status__c)
                    {
                    	if(applicationObject.Immigration_Status__c == 'Completed') {
                    		system.debug('id of applicant ->' + applicationObject.Name_of_Applicant__c);
                    		system.debug('passport number ->' + applicationObject.Passport_Number_on_Record__c);
                    		if(applicationObject.Passport_Number__c != applicationObject.Passport_Number_on_Record__c) {
	                    		Passport_History__c passportObject = new Passport_History__c();
								passportObject.Passport_Holder__c = applicationObject.Name_of_Applicant__c;                    		
								PassportObject.Passport_Number__c = applicationObject.Passport_Number__c;
								passportObject.Passport_Issue_Date__c = applicationObject.Passport_Issue_Date__c;
								passportObject.Passport_Expiry_Date__c = applicationObject.Passport_Expiry_Date__c;
								passportObject.Passport_Issue_Country__c = applicationObject.Passport_Country_of_Issue__c;
								passportObject.Passport_Issue_Place__c = applicationObject.Passport_Place_of_Issue__c;
								passportObject.Passport_Place_of_Issue_Arabic__c = applicationObject.Passport_Place_of_Issue_Arabic__c;
								passportObject.Place_of_Birth__c = applicationObject.Place_of_Birth__c;
								passportObject.Place_of_Birth_Arabic__c = applicationObject.Place_of_Birth_Arabic__c;
								passportObject.Passport_Type__c = applicationObject.Passport_Type__c;
								passportObject.isCurrent__c = true;
								 //Adding in the List...
	                       		passportHistoryInsertList.add(passportObject);
                    		} else {

                    			for(Passport_History__c passportObject: passHistoryMap.values()){
									PassportObject.Passport_Number__c = applicationObject.Passport_Number__c;
									passportObject.Passport_Issue_Date__c = applicationObject.Passport_Issue_Date__c;
									passportObject.Passport_Expiry_Date__c = applicationObject.Passport_Expiry_Date__c;
									passportObject.Passport_Issue_Country__c = applicationObject.Passport_Country_of_Issue__c;
									passportObject.Passport_Issue_Place__c = applicationObject.Passport_Place_of_Issue__c;
									passportObject.Passport_Place_of_Issue_Arabic__c = applicationObject.Passport_Place_of_Issue_Arabic__c;
									passportObject.Place_of_Birth__c = applicationObject.Place_of_Birth__c;
									passportObject.Place_of_Birth_Arabic__c = applicationObject.Place_of_Birth_Arabic__c;
									passportObject.Passport_Type__c = applicationObject.Passport_Type__c;
									passportObject.isCurrent__c = true;
									//Adding in the List...
	                       			passportHistoryUpsertList.add(passportObject);
	                       			//add to map
	                       			applicationPassportIdMap.put(applicationObject.id, PassportObject.id);
                    				
                    			}
                    		}	
                        
								Visa_History__c visaObject = new Visa_History__c();
								if(applicationObject.Passport_Number__c == applicationObject.Passport_Number_on_Record__c) {
									visaObject.Passport_History_Number__c = applicationPassportIdMap.get(applicationObject.id);
								}
		                        visaObject.Visa_Holder__c = applicationObject.Name_of_Applicant__c;							
								visaObject.Occupation__c = applicationObject.Job_Title__c;
								visaObject.Sponsor__c = applicationObject.Company__c;
								visaObject.Visa_Type__c = 'Residency Permit';
								visaObject.Visa_Package__c = 'Employment';
								visaObject.isCurrent__c = true;
								visaObject.Visa_Number__c = applicationObject.Residency_File_Number__c;
								visaObject.Visa_Issue_Date__c = applicationObject.Residency_Permit_Issue_Date__c;
								visaObject.Visa_Expiry_Date__c = applicationObject.Residency_Permit_Expiry_Date__c;
								visaObject.MOI_Number__c = integer.valueof(applicationObject.Unified_ID__c);
	                    		visaHistoryInsertList.add(visaObject);
                    	}
                    }
            }//end if residency 
        	
        	
        }
        
        if(visaHistoryEmploymentUpdateList.size() > 0)
	        update visaHistoryEmploymentUpdateList;
        
        
        if(passportHistoryInsertList.size() > 0 && visaHistoryInsertList.size() > 0){
        	insert passportHistoryInsertList;
        	
			//Now Inserting the Visa History and Updating the .... (Populating the Id for the Passport Number...)
            for(integer counter = 0; counter < passportHistoryInsertList.size() ; counter++)
            {
                //For Visa History 
                visaHistoryInsertList[counter].Passport_History_Number__c = passportHistoryInsertList[counter].Id;
                
            }
            
            //Now inserting the Visa History...
            insert visaHistoryInsertList;        	
        }
        
        if(passportHistoryUpsertList.size() > 0) {
        	upsert passportHistoryUpsertList;
        	
        	 //Now inserting the Visa History...
            insert visaHistoryInsertList;     
        }
        
         /** end employment and residency auto complete */   
        
    }
    
    if (Trigger.isUpdate && Trigger.isbefore) 
    {
        //Map<Id,RecordType> recordTypeMap = new Map<Id,RecordType>([Select id,SObjectType,IsActive,DeveloperName,name From RecordType where SObjectType =: 'Application__c' AND IsActive =: true AND DeveloperName =: 'Employment']);

        //To Associate this Contact with the Master Account...
        Account masterAccountObject = new Account();
        masterAccountObject = [select id from Account where Master_Account_Flag__c =: true LIMIT 1];
        
        for(Application__c applicationObject : Trigger.new)
        {
            //if(recordTypeMap.get(applicationObject.RecordTypeId).DeveloperName == 'Employment')
            if(applicationObject.Record_Type_Name__c == 'Employment')
            {
                if(applicationObject.isApproved__c != Trigger.oldMap.get(applicationObject.id).isApproved__c)
                {
                    if(applicationObject.isApproved__c == true)
                    {
                        //Creating the Contacts...                  
                        Contact contactObject = new Contact();
                        contactObject.AccountId = masterAccountObject.id;
                        contactObject.FirstName = applicationObject.Applicant_First_Name__c;
                        contactObject.LastName = applicationObject.Applicant_Last_Name__c;
                        //contactObject.Contact_Type__c ='Employee - Under Process';
                        contactObject.Birthdate = applicationObject.Date_of_Birth__c;
                        contactObject.Country_of_Birth__c = applicationObject.Country_of_Birth__c;
                        contactObject.Birth_Place__c = applicationObject.Place_of_Birth__c;
                        contactObject.Birth_Place_Arabic__c = applicationObject.Place_of_Birth_Arabic__c;
                        contactObject.Qualification__c = applicationObject.Qualification__c;
                        contactObject.Religion__c = applicationObject.Religion__c;
                        contactObject.International_Phone_Number__c = applicationObject.International_Mobile_Number__c;
                        contactObject.Address_English__c = applicationObject.International_Address__c;
                        contactObject.Address_Arabic__c = applicationObject.International_Address_Arabic__c;
                        contactObject.Email = applicationObject.Applicant_Email__c;
                        contactObject.Country_of_Residence__c = applicationObject.Permanent_Country__c;
                        contactObject.Previous_Nationality__c = applicationObject.Previous_Nationality__c;
                        contactObject.Mother_Name__c = applicationObject.Mothers_Name__c;
                        contactObject.Mother_Name_Arabic__c = applicationObject.Mothers_Name_Arabic__c;
                        contactObject.Marital_Status__c = applicationobject.Marital_Status__c;
                        contactObject.Current_Employer__c = applicationobject.Company__c;
                        contactObject.Nationality__c = applicationObject.Current_Nationality__c;
                        contactObject.Preferred_Language__c = applicationObject.Native_Language__c;
        
                        //Adding in the List...
                        contactInsertList.add(contactObject);
                        
                        //For Passport History...
                        Passport_History__c passportObject = new Passport_History__c();
                        passportObject.Passport_Number__c = applicationObject.Passport_Number__c;
                        passportObject.Passport_Issue_Date__c = applicationObject.Passport_Issue_Date__c;
                        passportObject.Passport_Expiry_Date__c = applicationObject.Passport_Expiry_Date__c;
                        passportObject.Passport_Issue_Country__c = applicationObject.Passport_Country_of_Issue__c;
                        passportObject.Passport_Issue_Place__c = applicationObject.Passport_Place_of_Issue__c;
                        passportObject.Passport_Place_of_Issue_Arabic__c = applicationObject.Passport_Place_of_Issue_Arabic__c;
                        passportObject.Place_of_Birth__c = applicationObject.Place_of_Birth__c;
                        passportObject.Place_of_Birth_Arabic__c = applicationObject.Place_of_Birth_Arabic__c;
                        passportObject.Passport_Type__c = applicationObject.Passport_Type__c;
                        passportObject.isCurrent__c = true;
                        //Adding in the List...
                        passportHistoryInsertList.add(passportObject);
                        
                        //For Visa History...
                        Visa_History__c visaObject = new Visa_History__c();
                        visaObject.Occupation__c = applicationObject.Job_Title__c;
                        visaObject.Sponsor__c = applicationObject.Company__c;
                        visaObject.Visa_Type__c = 'Entry Permit';
                        visaObject.Visa_Package__c = 'Employment';
                        visaObject.isCurrent__c = true;
                        
                        //Adding in the List...
                        visaHistoryInsertList.add(visaObject);                      
                    }
                    
                }
            }
            
        }  
        
        //Now Inserting the Records....
        if(contactInsertList.size() > 0)
        {
            //Inserting all the contacts...
            insert contactInsertList;
            
            //Now Inserting the Passport History Number and Visa...(Populating the Contact Ids in the Visa History and Passport History)
            for(integer counter = 0; counter < contactInsertList.size() ; counter++)
            {
                //For Passport Holder...
                passportHistoryInsertList[counter].Passport_Holder__c = contactInsertList[counter].Id;
                            
                //For Visa History 
                visaHistoryInsertList[counter].Visa_Holder__c = contactInsertList[counter].Id;
                
            }
            
            //Now Inserting the Passport Histories...
            insert passportHistoryInsertList;
            
            //Now Inserting the Visa History and Updating the .... (Populating the Id for the Passport Number...)
            for(integer counter = 0; counter < passportHistoryInsertList.size() ; counter++)
            {
                //For Visa History 
                visaHistoryInsertList[counter].Passport_History_Number__c = passportHistoryInsertList[counter].Id;
                
            }
            
            //Now inserting the Visa History...
            insert visaHistoryInsertList;
            
            //Its old appraoche when we did not implement the isCurrent Visa and Passport Approach...
            /*//Now Updating the Contact .... (Populating the Id for the Passport Number and Visa Reference Number...)
            for(integer counter = 0; counter < passportHistoryInsertList.size() ; counter++)
            {
                //Put the Passport History Id in Contact...
                contactInsertList[counter].Passport_Number__c = passportHistoryInsertList[counter].Id;
                
                //Put the Visa History Id in Contact...
                contactInsertList[counter].Visa_Reference_Number__c = visaHistoryInsertList[counter].id;
                
            }
            
            //Now Update the contact...
            update contactInsertList;*/
            
            
            //Assigning the New contacts to the Applications
            for (Application__c applicationObject : Trigger.new) 
            {
                for(integer i=0; i<contactInsertList.Size(); i++)
                {  
                   applicationObject.Name_of_Applicant__c = contactInsertList[i].Id;
                } 
            }
            
            
        }
            
    }      
    
    
    
    //For Establishment Card Related Information....
    if(applicationEstablistmentNewMap.size() > 0 || applicationEstablistmentNewFineMap.size() > 0)
    {
      
      Map<string, Account> tempAccountList = new Map<string,Account>();
      
      for(Account accountObject : [SELECT ID,Establishment_Card_Number__c , Company_Establishment_Date__c ,Establishment_Card_Issue_Date__c ,Establishment_Card_Expiry_Date__c FROM Account where ID IN: establishmentCardSet ])  
      {
        tempAccountList.put(accountObject.Id, accountObject);
      }
      
      //For Establishment New...
      for(Application__c applicationObject : applicationEstablistmentNewMap.values() )
      {
        string tempKey = applicationObject.Id + '' +  applicationObject.Company__c;
        
        tempAccountList.get(applicationObject.Company__c).Establishment_Card_Number__c = applicationObject.Establishment_Card_Number__c;
        tempAccountList.get(applicationObject.Company__c).Company_Establishment_Date__c  = System.today();
        tempAccountList.get(applicationObject.Company__c).Establishment_Card_Issue_Date__c  = applicationObject.Establishment_Card_Issue_Date__c; 
        tempAccountList.get(applicationObject.Company__c).Establishment_Card_Expiry_Date__c  = applicationObject.Establishment_Card_Expiry_Date__c;
        
        /*tempAccountList.get(tempKey).Establishment_Card_Number__c = applicationObject.Establishment_Card_Number__c;
        tempAccountList.get(tempKey).Company_Establishment_Date__c  = System.today();
        tempAccountList.get(tempKey).Establishment_Card_Issue_Date__c  = applicationObject.Establishment_Card_Issue_Date__c; 
        tempAccountList.get(tempKey).Establishment_Card_Expiry_Date__c  = applicationObject.Establishment_Card_Expiry_Date__c;
        */
        
      }
      
      //For Not Establishment New and Establistment Fine...
      for(Application__c applicationObject : applicationEstablistmentNewFineMap.values() )
      {
        string tempKey = applicationObject.Id + '' + applicationObject.Company__c;
        
        tempAccountList.get(applicationObject.Company__c).Establishment_Card_Number__c = applicationObject.Establishment_Card_Number__c;
        tempAccountList.get(applicationObject.Company__c).Establishment_Card_Issue_Date__c  = applicationObject.Establishment_Card_Issue_Date__c; 
        tempAccountList.get(applicationObject.Company__c).Establishment_Card_Expiry_Date__c  = applicationObject.Establishment_Card_Expiry_Date__c;
        
        /*tempAccountList.get(tempKey).Establishment_Card_Number__c = applicationObject.Establishment_Card_Number__c;
        tempAccountList.get(tempKey).Establishment_Card_Issue_Date__c  = applicationObject.Establishment_Card_Issue_Date__c; 
        tempAccountList.get(tempKey).Establishment_Card_Expiry_Date__c  = applicationObject.Establishment_Card_Expiry_Date__c;
        */
      }
      
      //For Updating The records ...
      if(tempAccountList.size() > 0)
        upsert tempAccountList.values();
      
    }
    
    //For Visa Related Maps...
    if(visaHistorySendImmigrationMap.size() > 0 || visaHistoryApprovedMap.size() > 0 || visaHistoryStopCompanyMap.size() > 0 || visaHistoryStopAfzaMap.size() > 0 || visaHistoryRejectMap.size() > 0)
    {
        for(Visa_History__c visaObject : [SELECT Id,Occupation__c, Sponsor__c,Visa_Type__c,Cancellation_Reason__c ,Visa_Package__c, isCurrent__c,Visa_Cancellation_Date__c,Apply_Date__c FROM Visa_History__c WHERE Id IN: visaHistoryRejectMap.keyset() OR ID IN: visaHistoryStopAfzaMap.keyset() OR Id IN:visaHistoryStopCompanyMap.keyset() OR Id IN:visaHistorySendImmigrationMap.keyset() OR Id IN:visaHistoryApprovedMap.keyset()])
        {
            if(visaHistoryStopCompanyMap.get(visaObject.Id) != null)
            {
                visaObject.Visa_Cancellation_Date__c = System.today();
                visaObject.Cancellation_Reason__c = 'Stop by Company';
            }
            else if(visaHistoryStopAfzaMap.get(visaObject.Id) != null)
            {
                visaObject.Visa_Cancellation_Date__c = System.today();
                visaObject.Cancellation_Reason__c = 'Stop by Free Zone';
            }
            else if(visaHistoryRejectMap.get(visaObject.Id) != null)
            {
                visaObject.Visa_Cancellation_Date__c = System.today();
                visaObject.Cancellation_Reason__c = 'Stop by Immigration';
            }
             
             else if(visaHistorySendImmigrationMap.get(visaObject.Id) != null)
            {
              visaObject.Apply_Date__c = System.today();
                visaObject.Visa_Cancellation_Date__c = null;
                visaObject.Cancellation_Reason__c = null;
            }
            
            else if(visaHistoryApprovedMap.get(visaObject.Id) != null)
            {
                visaObject.Visa_Cancellation_Date__c = null;
                visaObject.Cancellation_Reason__c = null;
            }
             
           
           
            //Adding in the Visa History Update List...
            visaHistoryUpdateList.add(visaObject);
        
        }
        
        
        //Now Update the Visa History...
        if(visaHistoryUpdateList.size() > 0)
            update visaHistoryUpdateList;
        
        
        
    }    
    
    /*Set<id> RectypIds = new Set<id>();
 
    List<Contact> ContactsToBeAdded = new List<Contact>();   
    List<Application__c> ApplicationsToBeUpdated = new List<Application__c>();
 
    Map<Id,RecordType> recordTypeMap = new Map<Id,RecordType>([Select id,name From RecordType]);

     if (Trigger.isInsert) 
     {
        for (Application__c item : Trigger.new) 
        {
          RecordType Rec = recordTypeMap.get(item.RecordTypeId);        
          if(Rec.name == 'Employment')
           { 
              if(item.Name_of_Applicant__c == null)
               {
                  Application__c app = item;
              
                  Contact con = new Contact(FirstName=app.Applicant_First_Name__c,LastName = app.Applicant_Last_Name__c,Contact_Type__c='Employee - Under Process', AccountId=app.Company__c );
                  
                  //Adding in the List...
                  ContactsTobeAdded.add(con);
               } 
           } 
        }   
      }
        //Inserting the new Contacts 
            upsert(ContactsTobeAdded);
            
            //Assigning the New contacts to the Applications
            for (Application__c item : Trigger.new) 
            {
                Application__c app = item;
                for(integer i=0; i<ContactsTobeAdded.Size(); i++)
                {  
                   app.Name_of_Applicant__c = ContactsTobeAdded[i].Id;
                } 
            }
      } 
     
     */     
           
           
}